package com.finger.forest;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.finger.forest.adapter.MyBaseExpandableListAdapter;
import com.finger.forest.adapter.ResourceTypeAdapter;
import com.finger.forest.base.BaseActivity;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.model.BaseCommonResourceModel;
import com.finger.forest.model.CollectionResourceModel;
import com.finger.forest.model.ResourceTypeModel;
import com.finger.forest.utils.ClickUtils;
import com.finger.forest.utils.LoadData;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.UniversalItemDecoration;
import com.finger.forest.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 采集资源类型
 */
public class CollectionResourceTypeActivity extends BaseActivity<RegisterPresenter> implements View.OnClickListener, BaseView {

    private static final String TAG = "CollectionResourceTypeActivity";

    /**
     * 返回
     */
    @BindView(R.id.btn_back)
    ImageView backIv;

    /**
     * 确定
     */
    @BindView(R.id.btn_confirm)
    Button confirmBtn;
    @BindView(R.id.rvType)
    RecyclerView rvType;

    private List<ResourceTypeModel> resourceTypeModels = new ArrayList<>();
    List<BaseCommonResourceModel> baseCommonResourceModels = new ArrayList<>();
    private ResourceTypeAdapter resourceTypeAdapter;

    private ResourceTypeModel.ChildrenBean mChildrenBean;

    @Override
    protected int setView() {
        return R.layout.activity_collection_resource_type;
    }

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeView() {
        resourceTypeAdapter = new ResourceTypeAdapter(baseCommonResourceModels);
        rvType.setAdapter(resourceTypeAdapter);

        GridLayoutManager lm = new GridLayoutManager(this, 2);
        lm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                int span = 0;
                switch (baseCommonResourceModels.get(i).getItemType()) {
                    case BaseCommonResourceModel.TYPE_PRENT:
                        span = 2;
                        break;
                    case BaseCommonResourceModel.TYPE_CHILD:
                        span = 1;
                        break;
                    default:
                        break;
                }
                return span;
            }
        });
        rvType.setLayoutManager(lm);

        rvType.addItemDecoration(new UniversalItemDecoration() {
            @Override
            public Decoration getItemOffsets(int position) {
                ColorDecoration decoration = new ColorDecoration();
                BaseCommonResourceModel baseCommonResourceModel = baseCommonResourceModels.get(position);
                decoration.decorationColor = ContextCompat.getColor(context, R.color.blackF5);
                if (baseCommonResourceModel.getItemType() == BaseCommonResourceModel.TYPE_PRENT) {
                    decoration.top = getResources().getDimensionPixelOffset(R.dimen.dp_1);
                    decoration.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_1);
                }

                if (baseCommonResourceModel.getItemType() == BaseCommonResourceModel.TYPE_CHILD) {
                    decoration.right = getResources().getDimensionPixelOffset(R.dimen.dp_1);
                    decoration.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_1);
                }
                return decoration;
            }
        });

        resourceTypeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                BaseCommonResourceModel resourceModel = baseCommonResourceModels.get(position);
                if (resourceModel.getItemType() == BaseCommonResourceModel.TYPE_CHILD) {
                    if (view.getId() == R.id.child_name) {
                        mChildrenBean = resourceModel.getChildrenBean();
                        if (null != mChildrenBean) {
                            resourceTypeAdapter.mPosition = position;
                            resourceTypeAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void loadDataFromServer() {
        if (Utils.isLoginOnLine(getApplicationContext())){
            //在线登录，请求接口
            loadRecourceType();
        } else {
            resourceTypeModels = Utils.getLocalData(CollectionResourceTypeActivity.this);
            loadDataAdapter();
        }

    }

    private void loadDataAdapter(){
        if (null != resourceTypeModels && resourceTypeModels.size() > 0) {
            baseCommonResourceModels.clear();
            for (ResourceTypeModel resourceModel : resourceTypeModels) {
                if (null != resourceModel) {
                    BaseCommonResourceModel commonResourceModel = new BaseCommonResourceModel();
                    commonResourceModel.setResourceTypeModel(resourceModel);
                    commonResourceModel.setType(BaseCommonResourceModel.TYPE_PRENT);
                    baseCommonResourceModels.add(commonResourceModel);

                    if (null != resourceModel.getChildren() && resourceModel.getChildren().size() > 0) {
                        for (ResourceTypeModel.ChildrenBean childrenBean : resourceModel.getChildren()) {
                            BaseCommonResourceModel model1 = new BaseCommonResourceModel();
                            model1.setChildrenBean(childrenBean);
                            model1.setType(BaseCommonResourceModel.TYPE_CHILD);
                            baseCommonResourceModels.add(model1);
                        }
                    }
                }
            }
        }

        resourceTypeAdapter.notifyDataSetChanged();
    }

    private void loadRecourceType() {
        Map<String, Object> maps = new HashMap<>();
        presenter.loadResourceType(maps);
    }

    @OnClick({R.id.btn_back, R.id.btn_confirm})
    @Override
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) { //防止重复点击
            switch (v.getId()) {

                //返回
                case R.id.btn_back:
                    finish();
                    break;

                //确定
                case R.id.btn_confirm:
                    if (null != mChildrenBean) {
                        Intent intent = new Intent();
                        intent.putExtra("mChildrenBean", mChildrenBean);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        ToastUtils.show("请选择资源类型");
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void onSuccess(Object model, int tag, String msg) {
        switch (tag) {
            case ResponseTag.RESOURCE_TYPE:
                List<ResourceTypeModel> resourceTypeModelList = (List<ResourceTypeModel>) model;
                if (null != resourceTypeModelList && resourceTypeModelList.size() > 0) {
                    resourceTypeModels.clear();
                    resourceTypeModels.addAll(resourceTypeModelList);
                }
                loadDataAdapter();
                break;

            default:
                break;
        }
    }

    @Override
    public void onError(int errcode, int tag, String message) {

    }
}
